package com.shop.shopserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shopcommon.context.SecurityContext;
import com.shop.shopcommon.enumeration.OrderStatusType;
import com.shop.shopcommon.enumeration.PayStatusType;
import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.UpdateOrderDTO;
import com.shop.shoppojo.entity.User;
import com.shop.shoppojo.entity.UserOrder;
import com.shop.shoppojo.vo.UpdateOrderVO;
import com.shop.shopserver.mapper.OrderMapper;
import com.shop.shopserver.mapper.UserMapper;
import com.shop.shopserver.service.UpdateOrderStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class UpdateOrderStatusServiceImpl extends ServiceImpl<OrderMapper, UserOrder> implements UpdateOrderStatusService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 支付订单
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO> payOrder(UpdateOrderDTO updateOrderDTO){
        //判断订单中有无Id订单

        String id = updateOrderDTO.getId();

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id);

        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info(""+userOrder);


        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(getUserIdByUsername().getId())) {
            userOrder.setPayStatus(PayStatusType.PAY);
            userOrder.setOrderStatus(OrderStatusType.UNSHIPPED);
            userOrder.setPayTime(LocalDateTime.now());
            orderMapper.update(userOrder,lqw);
        }
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);
        updateOrderVO.setId(userOrder.getId().toString());
        return Result.success(updateOrderVO,"订单已支付");

    }


    /**
     * 订单取消
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO> applyCancelOrder(UpdateOrderDTO updateOrderDTO){
        String id = updateOrderDTO.getId();
        log.info("将要删除的订单是"+id);

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id);
        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info("将要删除的订单的查询结果"+userOrder);

        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(getUserIdByUsername().getId())) {
            userOrder.setOrderStatus(OrderStatusType.CANCELED);
            userOrder.setCancelTime(LocalDateTime.now());
            orderMapper.update(userOrder,lqw);
        }
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);
        updateOrderVO.setId(userOrder.getId().toString());

        return Result.success(updateOrderVO,"订单已取消");
    }

    /**
     * 异步订单取消
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO>  asyncApplyCancelOrder(UpdateOrderDTO updateOrderDTO,Long userId){
        Long id = Long.valueOf(updateOrderDTO.getId());
        log.info("将要删除的订单是"+id);

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id);
        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info("将要删除的订单的查询结果"+userOrder);

        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(userId)) {
            userOrder.setOrderStatus(OrderStatusType.CANCELED);
            userOrder.setCancelTime(LocalDateTime.now());
            orderMapper.update(userOrder,lqw);
        }
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);

        return Result.success(updateOrderVO,"订单已取消");
    }


    /**
     * 订单收货
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO> confirmReceipt(UpdateOrderDTO updateOrderDTO){
        String id = updateOrderDTO.getId();

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id);
        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info(""+userOrder);

        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(getUserIdByUsername().getId())) {
            userOrder.setOrderStatus(OrderStatusType.UNCOMMENT);
            orderMapper.update(userOrder,lqw);
        }
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);
        updateOrderVO.setId(userOrder.getId().toString());
        return Result.success(updateOrderVO,"订单已收货");
    }


    /**
     * 订单发货
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO> deliverOrder(UpdateOrderDTO updateOrderDTO){
        String id = updateOrderDTO.getId();

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id);
        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info(""+userOrder);

        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(getUserIdByUsername().getId())) {
            userOrder.setOrderStatus(OrderStatusType.UNRECEIVED);
            orderMapper.update(userOrder,lqw);
        }
        //有订单 order_status=2->3
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);
        updateOrderVO.setId(userOrder.getId().toString());
        return Result.success(updateOrderVO,"订单已发货");
    }


    /**
     * 订单删除
     * @param updateOrderDTO
     * @return
     */
    public Result<UpdateOrderVO> deleteOrder(UpdateOrderDTO updateOrderDTO){
        String id = updateOrderDTO.getId();

        LambdaQueryWrapper<UserOrder> lqw = new LambdaQueryWrapper<UserOrder>()
                .eq(UserOrder::getId,id).in(UserOrder::getOrderStatus, 6,4);
        UserOrder userOrder = orderMapper.selectOne(lqw);
        log.info(""+userOrder);

        if(userOrder == null ){
            return Result.error("暂无此订单");
        } else if (userOrder.getUserId().equals(getUserIdByUsername().getId())) {
            orderMapper.delete(lqw);
        }
        UpdateOrderVO updateOrderVO = new UpdateOrderVO();
        BeanUtils.copyProperties(userOrder, updateOrderVO);
        updateOrderVO.setId(userOrder.getId().toString());

        return Result.success(updateOrderVO,"订单已删除");
    }

    /**
     * 根据用户名查询用户id
     * @return
     */
    private User getUserIdByUsername() {
        String username = SecurityContext.getUsername();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .select(User::getId)
                .eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("user" + user);
        return user;
    }
}
